using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace awcast
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private TreeNode TreeNodeFromXmlNode(System.Xml.XmlNode xn)
        {
            TreeNode tn;
            string txt="???";
            if (xn.Name != "outline") return null;
            tn = new TreeNode();
            foreach (System.Xml.XmlAttribute xa in xn.Attributes)
            {
                if (xa.Name == "text") txt = xa.Value;
            }
            tn.Text = txt;
            foreach (System.Xml.XmlNode xnn in xn.ChildNodes)
            {
                tn.Nodes.Add(TreeNodeFromXmlNode(xnn));
            }
            return tn;
        }

        private void podDirLoadDirectory(string url)
        {
            
            string podDirOPML;
            System.Xml.XmlNodeList nl;
            
            podDirOPML = webClient1.DownloadString(url);
            System.Xml.XmlDocument xmlDoc=new System.Xml.XmlDocument();
            xmlDoc.LoadXml(podDirOPML);
            nl = xmlDoc.GetElementsByTagName("body");
            foreach (System.Xml.XmlNode xn in nl[0].ChildNodes)
            {
                podDirTree.Nodes.Add(TreeNodeFromXmlNode(xn));
            }
            podDirLoading.Visible = false;    
            /*foreach (System.Xml.XmlNode xn in xmlDoc.ChildNodes)
            {
                
            }
            int i;
            int k;
            for (i=0;i<10;i++)
            {
                podDirTree.Nodes.Insert(i,i.ToString());
                for (k = 0; k < i; k++)
                {
                    podDirTree.Nodes[i].Nodes.Insert(k, k.ToString());
                }
            }*/
        }

        private void podDirLoadButton_Click(object sender, EventArgs e)
        {
            podDirLoading.Visible = true;
            //
            podDirWorker.RunWorkerAsync();
        }

        private void podDirWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            podDirLoadDirectory("http://podfeeder.com/allshows.opml");
            //podDirLoadDirectory("http://www.digitalpodcast.com/opml/digitalpodcast.opml");
            //podDirTree.Update();
        }
    }
}